package 链表;

import common.ListNode;

/**
 * @author tmh
 * @date 2024/11/22 19:07
 * @description
 */
public class T21合并两个有序链表 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode result = new ListNode();
        ListNode pre = result;
        while (list1 != null && list2 != null) {
            int num1 = list1.val;
            int num2 = list2.val;
            if (num1 >= num2) {
                result.next = list2;
                list2 = list2.next;
            } else {
                result.next = list1;
                list1 = list1.next;
            }
            result = result.next;
        }
        while (list1 != null) {
            result.next = list1;
            list1 = list1.next;
            result = result.next;
        }
        while (list2 != null) {
            result.next = list2;
            list2 = list2.next;
            result = result.next;
        }
        return pre.next;
    }
}
